import { defineStore } from 'pinia'
import { getProList, getPubList } from '@/api/super'

export const useDepartmentStore = defineStore('department', {
  state: () => ({
    proposalDepartments: [],
    socialDepartments: [],
    lastFetchTime: null,
    // cacheTimeout: 5 * 60 * 1000, // 缓存5分钟
  }),

  actions: {
    async fetchDepartments() {
      // // 如果数据已存在且在缓存时间内，直接返回
      // if (
      //   this.lastFetchTime && 
      //   Date.now() - this.lastFetchTime < this.cacheTimeout &&
      //   this.proposalDepartments.length && 
      //   this.socialDepartments.length
      // ) {
      //   return
      // }

      try {
        const [proRes, pubRes] = await Promise.all([
          getProList(),
          getPubList()
        ])
        
        if (proRes.code === 200) {
          this.proposalDepartments = proRes.data
        }
        if (pubRes.code === 200) {
          this.socialDepartments = pubRes.data
        }
        
        this.lastFetchTime = Date.now()
      } catch (error) {
        console.error('获取部门列表失败:', error)
        throw error
      }
    }
  }
}) 